package com.sinopac.util;

import java.io.File;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import com.sinopac.model.TokenSynchronous.TokenSynchronousModel;

public class XmlUtil {
	//檔案在WebContext/WEB-INF/OTPServer.xml
	
	private String filePath = "/OTPServer.xml";
	public String otpIP;
	public String otpPort;
	public String otpIssuer;
	public String userDBname;
	public static final String OTP_IP = "otpIP";
	public static final String OTP_PORT = "otpPort";
	public static final String OTP_ISSUER = "otpIssuer";
	public static final String USER_DB_NAME= "userDBname";
	public HashMap<String,String> map;
	
	public HashMap<String,String> otpServer(){
		
		String path = XmlUtil.class.getResource(filePath).getFile();
		path = path.replaceAll("%20", " ");
		
		System.out.println("path:"+path);
		
		File f = new File(path);
		
		System.out.println("f.getPath():"+f.getPath());
		
		//從設定檔取得OTPserver的issuer
		otpIssuer = getOTPissuer_func(f);
		//從設定檔取得OTPserver的IP
		otpIP = getOTPIP_func(f);
		//從設定檔取得OTPserver的password
		otpPort = getOTPPort_func(f);
		//從設定檔取得userDB的name
		userDBname = getUSER_DB_NAME_func(f);
		
		
		System.out.println("otpPort:"+otpPort);
		System.out.println("userDBname:"+userDBname);
		
		map = new HashMap<String, String>();
		map.put("otpIssuer", otpIssuer);
		map.put("otpIP", otpIP);
		map.put("otpPort", otpPort);
		map.put("userDBname", userDBname);
		
		return map;
	}
	
	public String getUSER_DB_NAME_func(File f2) {
		String s = "";
		try{ 	
			DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); 
			DocumentBuilder builder=factory.newDocumentBuilder(); 
			Document doc = builder.parse(f2); 
			NodeList nl = doc.getElementsByTagName("VALUE"); 
			s = doc.getElementsByTagName("USERDBNAME").item(0).getFirstChild().getNodeValue();
		}catch(Exception e){ 
		e.printStackTrace(); 
		}
		return s;
	}

	public String getOTPissuer_func(File f2) {
		String s = "";
		try{ 	
			DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); 
			DocumentBuilder builder=factory.newDocumentBuilder(); 
			Document doc = builder.parse(f2); 
			NodeList nl = doc.getElementsByTagName("VALUE"); 
			s = doc.getElementsByTagName("ISSUER").item(0).getFirstChild().getNodeValue();
		}catch(Exception e){ 
		e.printStackTrace(); 
		}
		return s;
	}
	
	public String getOTPIP_func(File f2){ 
		String s = "";
		try{ 	
			DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); 
			DocumentBuilder builder=factory.newDocumentBuilder(); 
			Document doc = builder.parse(f2); 
			NodeList nl = doc.getElementsByTagName("VALUE"); 
			s = doc.getElementsByTagName("OTPIP").item(0).getFirstChild().getNodeValue();
		}catch(Exception e){ 
		e.printStackTrace(); 
		}
		return s;
	}
	
	public String getOTPPort_func(File f2){ 
		//long lasting =System.currentTimeMillis(); 
		String s = "";
		try{ 	
			DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); 
			DocumentBuilder builder=factory.newDocumentBuilder(); 
			Document doc = builder.parse(f2); 
			NodeList nl = doc.getElementsByTagName("VALUE"); 
			s = doc.getElementsByTagName("PORT").item(0).getFirstChild().getNodeValue();
		}catch(Exception e){ 
		e.printStackTrace(); 
		}
		return s;
	}
	

	public String getOtpIP() {
		return otpIP;
	}


	public String getOtpPort() {
		return otpPort;
	}

	public String getOtpIssuer() {
		return otpIssuer;
	}
	
	public String getUserDBname(){
		return userDBname;
	}

}
